﻿namespace BM.Tools.WebControls.MForm.Controls.ValueFormatters
{    
    using System.ComponentModel;    
    using System.Diagnostics.CodeAnalysis;
    using System.Text.RegularExpressions;

    /// <summary>
    /// A class that performs a regex replacement
    /// </summary>
    public class RegexReplacerFormatter : StringFormatter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="RegexReplacerFormatter"/> class.
        /// </summary>
        public RegexReplacerFormatter()
        {
            Count = -1;
            Options = RegexOptions.None;
        }

        /// <summary>
        /// Gets or sets the MatchEvaluator which evaluates a replacement at each step.
        /// Can be used instead of the <see cref="Replacement" /> property.
        /// </summary>
        [SuppressMessage("Microsoft.Design", "CA1009:DeclareEventHandlersCorrectly", Justification = "MatchEvaluator cannot be changed and is used as an event as it simplifies method creation")]
        public event MatchEvaluator MatchEvaluation;

        /// <summary>
        /// Gets or sets the regular expression pattern to match.
        /// </summary>
        public string Pattern
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the a bitwise OR combination of Regex option values.
        /// </summary>
        [DefaultValue(RegexOptions.None)]
        public RegexOptions Options
        {
            get;
            set;
        }       

        /// <summary>
        /// Gets or sets the replacement string.
        /// Can be used instead of the <see cref="MatchEvaluator" /> property.
        /// </summary>
        public string Replacement
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the maximum number of times the replacement will occur.
        /// If -1, the maximum number is unlimited.
        /// </summary>
        [DefaultValue(-1)]
        public int Count
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the character position in the input string where the search will begin.
        /// </summary>
        [DefaultValue(0)]
        public int StartAt
        {
            get;
            set;
        }

        /// <summary>
        /// Performs a regex replacement on the input value
        /// </summary>
        /// <param name="value">
        /// The value.
        /// </param>
        /// <returns>
        /// the formatted string
        /// </returns>
        public override string Format(string value)
        {            
            var regex = new Regex(Pattern, Options);
            if (MatchEvaluation != null)
            {
                return regex.Replace(value, MatchEvaluation, Count, StartAt);
            }

            return regex.Replace(value, Replacement, Count, StartAt);
        }
    }
}
